package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProcureApplyBean;
import com.project.bean.ProcureApplyInfoBean;
import com.project.bean.PurchaseAuditRecordBean;
import com.project.bean.WaitProductBean;
import com.project.dto.WaitProductDto;
import com.project.mapper.IProcureApplyInfoMapper;
import com.project.mapper.IProcureApplyMapper;
import com.project.mapper.IPurchaseAuditRecordMapper;
import com.project.service.IProcureApplyService;
import com.project.util.SnowIdUtils;
import com.project.vo.ProcureApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ProcureApplyServiceImpl implements IProcureApplyService {
    @Autowired
    private IProcureApplyMapper procureApplyMapper;
    @Autowired
    private IProcureApplyInfoMapper procureApplyInfoMapper;

    @Override
    public String addProcureApply(ProcureApplyBean procureApplyBean) {
        if (procureApplyBean ==null){//判断采购申请是否为空
            return "采购申请为空对象";
        }
        procureApplyBean.setProcureApplyId(SnowIdUtils.uniqueLongHex());
        procureApplyMapper.insert(procureApplyBean);
        return "ok";
    }

    @Override
    public String addProcureApplyInfo(List<ProcureApplyInfoBean> procureApplyInfoList) {
        if (procureApplyInfoList==null || procureApplyInfoList.size()==0){//判断采购明细是否为空
            return "no";
        }
        //添加采购明细
        procureApplyInfoList.forEach(procureApplyInfoBean -> {
            procureApplyInfoMapper.insert(procureApplyInfoBean);
        });
        return "ok";
    }

    @Override
    public void addProcureApplyInfoOne(ProcureApplyInfoBean procureApplyInfoBean) {
        procureApplyInfoMapper.insert(procureApplyInfoBean);
    }


    @Override
    public String updateStatus(String procureApplyId, int status) {
//        if (procureApplyId==null || procureApplyId==""){//判断采购申请编号是否为空
//            return "申请编号不能为空";
//        }
//        if (status !=0&&status !=1&&status !=2){//判断审核状态
//            return "状态不符要求";
//        }
        ProcureApplyBean procureApplyObj= procureApplyMapper.selectById(procureApplyId);
//        if (procureApplyObj==null){//采购申请对象为空
//            return "采购申请编号输入有误";
//        }
        //修改审核状态
        procureApplyObj.setStatus(status);
        procureApplyMapper.updateById(procureApplyObj);
        return "ok";
    }

    @Override
    public String updateAuditReason(String procureApplyId, String auditReason) {
//        if (procureApplyId==null || procureApplyId==""){//判断采购申请编号是否为空
//            return "申请编号不能为空";
//        }
//        if (auditReason==null || auditReason==""){//判断审核原因是否为空
//            return "审核原因不能为空";
//        }
        ProcureApplyBean procureApplyObj= procureApplyMapper.selectById(procureApplyId);
//        if (procureApplyObj==null){//采购申请对象为空
//            return "采购申请编号输入有误";
//        }
        //修改审核未通过原因
        procureApplyObj.setAuditReason(auditReason);
        procureApplyMapper.updateById(procureApplyObj);
        return "ok";
    }

    @Override
    public IPage<ProcureApplyVo> cutByItem(int page) {//分页查询采购申请表
        Page<ProcureApplyBean> page11 = new Page<>(page, 3);
        IPage<ProcureApplyBean>  procureApplyBeanIPage=procureApplyMapper.selectPage(page11,null);
        // 将查询结果的ProcureApplyBean转换为ProcureApplyVO
        List<ProcureApplyVo>procureApplyVos=procureApplyBeanIPage.getRecords().stream().map(bean -> {
            String status=null;
            if (bean.getStatus()==0){
                status="未审核";
            }
            if (bean.getStatus()==1){
                status="审核通过";
            }
            if (bean.getStatus()==2){
                status="审核未通过";
            }
            ProcureApplyVo dto=new ProcureApplyVo(
                    bean.getProcureApplyId(),
                    bean.getProcureDate(),
                    bean.getReason(),
                    status,
                    bean.getAuditReason());
            return dto;
        }).collect(Collectors.toList());
        // 将转换后的WaitProductDto放到IPage对象中返回
        Page<ProcureApplyVo> page1 = new Page<>(page11.getCurrent(), page11.getSize(), procureApplyBeanIPage.getTotal());
        page1.setRecords(procureApplyVos);
        return page1;
    }

    @Override
    public ProcureApplyBean findByProcureApplyId(String procureApplyId) {//查询采购详情及明细
        if (procureApplyId==null || procureApplyId==""){//判断采购申请编号是否为空
            return null;
        }
        //查询申请
        ProcureApplyBean procureApplyObj=procureApplyMapper.selectById(procureApplyId);
        if (procureApplyObj==null){//申请对象为空
            return null;
        }
        //查询申请明细
        QueryWrapper<ProcureApplyInfoBean> qw=new QueryWrapper<>();
        qw.eq("procureApplyId",procureApplyId);
        procureApplyObj.setProcureApplyInfoList(procureApplyInfoMapper.selectList(qw));
        return procureApplyObj;
    }

    @Override
    public String updateProcureNum(String productId, int newProductNum) {//修改采购商品数量
        if (productId ==null || productId ==""){//商品编号为空返回
            return "商品编号为空";
        }
        if (newProductNum<0){//修改数量不能为负数
            return "商品数量应大于0";
        }
        //通过商品编号查询商品
        QueryWrapper<ProcureApplyInfoBean> qw=new QueryWrapper<>();
        qw.eq("productId",productId);
        ProcureApplyInfoBean procureApplyInfoBean=procureApplyInfoMapper.selectOne(qw);
        if (procureApplyInfoBean ==null){//判断商品是否为空
            return "未查询到商品，请确认商品编号是否正确";
        }
        //修改商品数量
        procureApplyInfoBean.setNum(newProductNum);
        procureApplyInfoMapper.updateById(procureApplyInfoBean);
        return "ok";
    }
}
